//
//  CarRadioViewController.m
//  Car
//
//  Created by 何晓文 on 15/9/17.
//  Copyright (c) 2015年 朱鸿儒. All rights reserved.
//

#import "CarRadioViewController.h"
#import "RadioView.h"
#import "BlurImageView.h"
#import "LORequestManger.h"
#import "RadioPlayTool.h"
#import "RadioListView.h"
#import "RadioListViewCell.h"
#import "BarrageView.h"
#import "BarrageModel.h"


#define kBarrageURL @"http://app.api.autohome.com.cn/autov4.8.8/news/radiocomments-pm1-p1%@-s10-lastid0.json"
#define kRadioM3u8 @"http://live.fm.autohome.com.cn/auto.m3u8"

@interface CarRadioViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) RadioView *radioView;
@property (nonatomic, strong) NSMutableArray *radioList;

@property (nonatomic, strong) RadioListView *listView;
@property (nonatomic,strong) BlurImageView *blur;
@property (nonatomic,strong) BarrageView *barrageView;

@property (nonatomic, strong) UITableView *table;
@property (nonatomic,strong) NSMutableArray *barrageArr;
@property (nonatomic,strong) NSTimer *timer;

@property (nonatomic,assign) BOOL isShowBarrage;
@end

@implementation CarRadioViewController

#pragma mark <-<-<-----给界面赋值----->->->

//获取当前时间
- (NSString *)getCurrentTime {
	NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
	[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
	NSString *time = [formatter stringFromDate:[NSDate date]];
	NSLog(@"%@",time);
	
	return time;
}

//给界面赋值
- (void)setValueForView {
	[self.radioView.programPic sd_setImageWithURL:[NSURL URLWithString:self.radioModel.radiopicurl] placeholderImage:[UIImage imageNamed:@"placeHolder.jpg"]];
	self.radioView.TitleLabel.text = self.radioModel.title;
	self.radioView.AlbumLabel.text = self.radioModel.album;
	self.radioView.programTime.text = self.radioModel.programtime;
	[self.blur sd_setImageWithURL:[NSURL URLWithString:self.radioModel.radiopicurl]];

	if (kRadioTool.isPlaying == YES) {
		[self startRotationWithImageView];
	}
	
	[self.table reloadData];
	
}

#pragma mark <-<-<-<-------播放开始------->->->->
- (void)playRadio {
	[self getData];
	[kRadioTool startPlayRadio];
	[self startRotationWithImageView];
	[self refreshPlayButton];
	
	
}
//接收单例数据
- (void)getData {
	
	//接收数据
	self.radioList = kRadioTool.radioList;
	//比较时间列表
	[kRadioTool compareTime];
	//接收当前播放数据
	self.radioModel = kRadioTool.radioModel;
	//给界面赋值
	[self setValueForView];
	//刷新节目单
	
	
	
	[self.listView.listMenu reloadAllComponents];
	//刷新播放按钮状态
	[self refreshPlayButton];
	//刷新弹幕按钮
	[self refreshBarrageButton];
}
- (void)pauseAndPlay {
	
	if (kRadioTool.isPlaying == YES) {
		[kRadioTool radioPause];
		[self refreshPlayButton];
		[self.radioView.programPic.layer removeAllAnimations];
		
	}else {
		[kRadioTool startPlayRadio];
		[self startRotationWithImageView];
		[self refreshPlayButton];
		
	}
	
}
- (void)refreshPlayButton {
	if (kRadioTool.isPlaying == YES) {

		[self.radioView.startButton setImage:[UIImage imageNamed:@"ic_pause"] forState:(UIControlStateNormal)];
		
	}else {
		[self.radioView.startButton setImage:[UIImage imageNamed:@"ic_play"] forState:(UIControlStateNormal)];
		
	}

}
#pragma mark <-<-<-<-------弹幕相关------->->->->
//解析弹幕
- (void)initBarrageData {
	
	[LORequestManger GET:[NSString stringWithFormat:kBarrageURL,[self getCurrentDate]] success:^(id response) {
		NSDictionary * dict = (NSDictionary *)response;
		NSDictionary *result = dict[@"result"];
		for (NSDictionary *content in result[@"list"]) {
			BarrageModel *model = [BarrageModel objectWithKeyValues:content];
			[self.barrageArr addObject:model];
		}
		[self displayBarrage];
	} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
		NSLog(@"%@",error);
	}];
	
}
//获取当前时间
- (NSString *)getCurrentDate {
	NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
	[formatter setDateFormat:@"yyyyMMdd"];
	NSString *date = [formatter stringFromDate:[NSDate date]];
	NSLog(@"%@",date);
	
	return date;
}
//按钮开启弹幕
- (void)showBarrage {
	//如果弹幕已显示
	if (self.isShowBarrage) {
		[self.barrageView removeFromSuperview];
		[self.timer invalidate];
		self.timer = nil;
		self.isShowBarrage = NO;
	}else {
		
		//加载view
		self.barrageView = [[BarrageView alloc] initWithFrame:CGRectMake(0, 64, kWidth, kHeight * 0.55)];
		self.barrageView.backgroundColor = [UIColor clearColor];
		[self.view addSubview:self.barrageView];
		//开启弹幕刷新
		[self startTimer];
		
		//初始化弹幕数据
		[self initBarrageData];
		
		self.isShowBarrage = YES;
	}
	//刷新弹幕按钮状态
	[self refreshBarrageButton];
}
- (void)refreshBarrageButton {
	if (self.isShowBarrage) {
		[self.radioView.moreButton setImage:[UIImage imageNamed:@"danhigh"] forState:(UIControlStateNormal)];
	}else {
		[self.radioView.moreButton setImage:[UIImage imageNamed:@"dannormal"] forState:(UIControlStateNormal)];
	}


}
- (void)startTimer {
	
	self.timer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(initBarrageData) userInfo:nil repeats:YES];
	
}
//显示弹幕
- (void)displayBarrage {
	
	[self.barrageView.commentArray  removeAllObjects];
	for (BarrageModel *bModel in self.barrageArr) {
		[self.barrageView.commentArray addObject:bModel.content];
	}
	[self.barrageView openBarrage];
	self.isShowBarrage = YES;
	
}

#pragma mark <-<-<-<-------viewDidLoad------->->->->


- (void)viewDidLoad {
	[super viewDidLoad];
	self.blur = [[BlurImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
	self.view = self.blur;
	self.view.backgroundColor = [UIColor clearColor];
	self.radioView = [[RadioView alloc]initWithFrame:[UIScreen mainScreen].bounds];
	self.view.backgroundColor = [UIColor clearColor];
	[self.view addSubview:self.radioView];
	
	self.listView.userInteractionEnabled = YES;
	self.view.userInteractionEnabled = YES;
	
	
	if (kRadioTool.radioModel == nil) {
		[ProgressHUD show:@"loading" Interaction:YES];
		[kRadioTool initData];
	}
	[self getData];
//	[self refreshBarrageButton];
	self.title = @"汽车电台";
	//创建弹出菜单
	self.listView = [[RadioListView alloc] initWithFrame:CGRectMake(0, kHeight, kWidth, kHeight * 0.55)];
	self.listView .backgroundColor = [UIColor blackColor];
	[self.view addSubview:self.listView];
	
	
	//设置内容视图
	self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, self.listView.height * 0.15, kWidth, self.listView.height *0.7)];
	self.table.backgroundColor = [UIColor clearColor];
	[self.listView addSubview:self.table];
	self.table.dataSource = self;
	self.table.delegate = self;
	
	//缓冲成功通知
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playRadio) name:@"readyToPlay" object:nil];
	
	[self.radioView.programList addTarget:self action:@selector(addRadioListView) forControlEvents:(UIControlEventTouchUpInside)];
	//播放按钮监听方法
	[self.radioView.startButton addTarget:self action:@selector(pauseAndPlay) forControlEvents:(UIControlEventTouchUpInside)];
	[self.listView.backButton addTarget:self action:@selector(dismissMenu) forControlEvents:(UIControlEventTouchUpInside)];
	//跟多按钮监听方法
	[self.radioView.moreButton addTarget:self action:@selector(showBarrage) forControlEvents:(UIControlEventTouchUpInside)];
	
	
}
//弹出节目菜单
- (void)addRadioListView {
	self.listView.listMenu.hidden = NO;
	[UIView animateWithDuration:0.6 animations:^{
		self.listView.y = kHeight * 0.45 - 49;
	}];
	
}
//菜单返回方法
- (void)dismissMenu {
	[UIView animateWithDuration:0.6 animations:^{
		self.listView.y = kHeight;
		
	} completion:^(BOOL finished) {
		self.listView.listMenu.hidden = YES;
	}];
	
	
	NSLog(@"返回");
}

#pragma mark <-<-<-<-------UItableViewDelegate------->->->->
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 40;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	
	return 1;
	
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	
	return  self.radioList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	RadioListModel *model= self.radioList[indexPath.row];
	RadioListViewCell *cell = [RadioListViewCell cellWithTableView:tableView];
	cell.backgroundColor = [UIColor maroonColor];
	[cell setValueWithCurrentTime:[self getCurrentTime] listModel:model];
	return cell;
}

#pragma mark ----旋转动画----
////开始旋转
- (void)startRotationWithImageView{
 CABasicAnimation* rotationAnimation;
	rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
	rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
	rotationAnimation.duration = 6;
	rotationAnimation.cumulative = YES;
	rotationAnimation.repeatCount = MAXFLOAT;
	[self.radioView.programPic.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
	
}
//懒加载数组
- (NSMutableArray *)radioList {
	if (!_radioList) {
		_radioList = [[NSMutableArray alloc] init];
	}
	return  _radioList;
}
- (NSMutableArray *)barrageArr {
	if (!_barrageArr) {
		_barrageArr = [[NSMutableArray alloc] init];
	}
	return _barrageArr;
}
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:YES];
	[ProgressHUD dismiss];

}
@end
